package aliyun.cx.admin.controller;

import aliyun.cx.admin.entity.Article;
import aliyun.cx.admin.service.ArticleService;
import aliyun.cx.admin.util.ConfigUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 文章管理控制器
 */
@RestController
@RequestMapping("/api/admin/articles")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;
    
    @Autowired
    private ConfigUtil configUtil;
    
    /**
     * 获取文章列表
     */
    @GetMapping
    public Map<String, Object> getArticleList(@RequestParam(defaultValue = "1") Integer current,
                                             @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Article> page = articleService.getArticlePage(current, size);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("message", "success");
            result.put("data", page);
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "获取文章列表失败：" + e.getMessage());
            return result;
        }
    }
    
    /**
     * 根据ID获取文章
     */
    @GetMapping("/{id}")
    public Map<String, Object> getArticleById(@PathVariable Long id) {
        try {
            // 获取包含标签的文章信息
            Article article = articleService.getArticleWithTags(id);
            
            Map<String, Object> result = new HashMap<>();
            if (article != null) {
                result.put("code", 200);
                result.put("message", "success");
                result.put("data", article);
            } else {
                result.put("code", 404);
                result.put("message", "文章不存在");
            }
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "获取文章失败：" + e.getMessage());
            return result;
        }
    }
    
    /**
     * 创建文章
     */
    @PostMapping
    public Map<String, Object> createArticle(@RequestBody Article article) {
        try {
            // 检查文章发布功能是否启用
            if (!configUtil.isArticlePublishEnabled()) {
                Map<String, Object> result = new HashMap<>();
                result.put("code", 403);
                result.put("message", "文章发布功能已禁用");
                return result;
            }
            
            boolean success = articleService.createArticle(article);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "创建成功");
                result.put("data", article);
            } else {
                result.put("code", 500);
                result.put("message", "创建失败");
            }
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "创建文章失败：" + e.getMessage());
            return result;
        }
    }
    
    /**
     * 更新文章
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateArticle(@PathVariable Long id, @RequestBody Article article) {
        try {
            article.setId(id);
            boolean success = articleService.updateArticle(article);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "更新成功");
                result.put("data", article);
            } else {
                result.put("code", 500);
                result.put("message", "更新失败");
            }
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "更新文章失败：" + e.getMessage());
            return result;
        }
    }
    
    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteArticle(@PathVariable Long id) {
        try {
            boolean success = articleService.deleteArticle(id);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", "删除成功");
            } else {
                result.put("code", 500);
                result.put("message", "删除失败");
            }
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "删除文章失败：" + e.getMessage());
            return result;
        }
    }
    
    /**
     * 切换发布状态
     */
    @PutMapping("/{id}/publish")
    public Map<String, Object> togglePublishStatus(@PathVariable Long id, @RequestBody Map<String, Boolean> request) {
        try {
            Boolean isPublished = request.get("isPublished");
            if (isPublished == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("code", 400);
                result.put("message", "缺少发布状态参数");
                return result;
            }
            
            boolean success = articleService.togglePublishStatus(id, isPublished);
            
            Map<String, Object> result = new HashMap<>();
            if (success) {
                result.put("code", 200);
                result.put("message", isPublished ? "发布成功" : "取消发布成功");
            } else {
                result.put("code", 500);
                result.put("message", "操作失败");
            }
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "切换发布状态失败：" + e.getMessage());
            return result;
        }
    }
}
